﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using MWS.Processing.Interfaces;

namespace MWS.Processing.Serializers
{
    public class ByteArraySerializer : IByteArraySerializer
    {

        public byte[] Serialize<T>(T item)
        {
            var formatter = new BinaryFormatter();
            var stream = new MemoryStream();
            formatter.Serialize(stream, item);
            stream.Seek(0, 0);
            return stream.ToArray();
        }

        public T Deserialize<T>(byte[] item)
        {
            var formatter = new BinaryFormatter();
            var stream = new MemoryStream();
            stream.Write(item, 0, item.Length);
            stream.Seek(0, 0);
            return (T)formatter.Deserialize(stream);
        }
    }
}